package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.MemberQuestionEntity;
import com.hy.admin.entity.QuestionEntity;
import com.hy.admin.enums.EnumIsChoice;
import com.hy.admin.enums.EnumQuestionState;
import com.hy.admin.helper.request.QuestionRequest;
import com.hy.admin.helper.response.QuestionResponse;
import com.hy.admin.service.QuestionService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by yangqq on 18/4/5.
 * 个人中心
 */
@RestController
@RequestMapping("/api/question")
public class QuestionController {

    protected final Logger logBase = LoggerFactory.getLogger(QuestionController.class);
    @Autowired
    private QuestionService questionService;

    /**
     * 用户的问题列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/own", method = RequestMethod.POST)
    public ApiResponseDTO own(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_own_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByMemberId(
                questionRequest.getMemberId(), null, questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_own_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 用户的问题列表---未回答
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/ownAsk", method = RequestMethod.POST)
    public ApiResponseDTO ownAsk(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_ownAsk_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByMemberId(
                questionRequest.getMemberId(), EnumQuestionState.ASK.getId(), questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_ownAsk_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 用户的问题列表---已回答
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/ownAnswered", method = RequestMethod.POST)
    public ApiResponseDTO ownAnswered(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_ownAnswered_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByMemberId(
                questionRequest.getMemberId(), EnumQuestionState.ANSWER.getId(), questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_ownAnswered_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 教师的问题列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/teacher", method = RequestMethod.POST)
    public ApiResponseDTO teacher(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_teacher_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByTeacherId(
                questionRequest.getMemberId(), null, questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_teacher_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 教师的问题列表---未回答
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/teacherAsk", method = RequestMethod.POST)
    public ApiResponseDTO teacherAsk(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_teacherAsk_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByTeacherId(
                questionRequest.getMemberId(), EnumQuestionState.ASK.getId(), questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_teacherAsk_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 教师的问题列表---已回答
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/teacherAnswered", method = RequestMethod.POST)
    public ApiResponseDTO teacherAnswered(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_teacherAnswered_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByTeacherId(
                questionRequest.getMemberId(), EnumQuestionState.ANSWER.getId(), questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_teacherAnswered_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 精选问题列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/choice", method = RequestMethod.POST)
    public ApiResponseDTO choice(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_choice_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByIsChoice(
                questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_choice_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 教师已回答公开提问问题列表
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/teacherPublic", method = RequestMethod.POST)
    public ApiResponseDTO teacherPublic(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_teacherPublic_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<List<MemberQuestionEntity>> questionResponse = questionService.listByTeacherIdAndIsPublic(
                questionRequest.getMemberId(), questionRequest.getPageNum(), questionRequest.getPageSize());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_teacherPublic_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 问题详情
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResponseDTO detail(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_detail_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);
        //获取数据
        ServicesResult<QuestionResponse> questionResponse = questionService.detail(questionRequest.getMemberId(),questionRequest.getQuestionId());
        apiResponseDTO.initServicesResult(questionResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(questionResponse.getResult()));
        logBase.info("question_detail_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 提问
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponseDTO add(ApiRequestDTO apiRequestDTO,@RequestParam(required = false ,value = "files") MultipartFile[] files) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_add_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        QuestionEntity questionEntity= gson.fromJson(apiRequestDTO.getRequestData(), QuestionEntity.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = questionService.add(questionEntity, files);
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("question_add_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 取消精品问题
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/cancelChoice", method = RequestMethod.POST)
    public ApiResponseDTO cancelChoice(@RequestBody ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_cancelChoice_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = questionService.updateChoice(questionRequest.getQuestionId(), EnumIsChoice.NO.getId());
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("question_cancelChoice_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 设置精品问题
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/setChoice", method = RequestMethod.POST)
    public ApiResponseDTO setChoice(@RequestBody ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_setChoice_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = questionService.updateChoice(questionRequest.getQuestionId(),EnumIsChoice.YES.getId());
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("question_setChoice_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 点赞
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public ApiResponseDTO like(@RequestBody ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("question_setChoice_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = questionService.like(questionRequest.getQuestionId(),questionRequest.getMemberId());
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("question_setChoice_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
